package com.atlin.project.service.task.impl;

import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.task.TaskTagsMapper;
import com.atlin.project.model.domain.task.TaskTags;
import com.atlin.project.model.dto.taskTags.TaskTagsQueryRequest;
import com.atlin.project.model.vo.TaskTagsVO;
import com.atlin.project.service.task.TaskTagsService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description taskTags
 * @createDate date
 */
@Service
public class TaskTagsServiceImpl extends ServiceImpl<TaskTagsMapper, TaskTags>
        implements TaskTagsService {

    @Override
    public void valid(TaskTags taskTags, boolean add) {

    }

    @Override
    public TaskTagsVO getTaskTagsVO(TaskTags taskTags, HttpServletRequest request) {
        TaskTagsVO taskTagsVO = TaskTagsVO.objToVo(taskTags);
        // todo other operation
        return taskTagsVO;
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param taskTagsQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<TaskTags> getQueryWrapper(TaskTagsQueryRequest taskTagsQueryRequest) {
        QueryWrapper<TaskTags> queryWrapper = new QueryWrapper<>();
        if (taskTagsQueryRequest == null) {
            return queryWrapper;
        }
        Long id = taskTagsQueryRequest.getId();
        String sortField = taskTagsQueryRequest.getSortField();
        String sortOrder = taskTagsQueryRequest.getSortOrder();
        String searchText = taskTagsQueryRequest.getSearchText();
        Long taskId = taskTagsQueryRequest.getTaskId();
        String tagName = taskTagsQueryRequest.getTagName();



        queryWrapper.like(StringUtils.isNotBlank(searchText), "tagName", searchText);
        queryWrapper.like(StringUtils.isNotBlank(tagName), "tagName", tagName);
        queryWrapper.eq(ObjectUtils.isNotEmpty(taskId), "taskId", taskId);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        return queryWrapper;
    }

    @Override
    public Page<TaskTagsVO> getTaskTagsVOPage(Page<TaskTags> taskTagsPage, HttpServletRequest request) {
        List<TaskTags> taskTagsList = taskTagsPage.getRecords();
        Page<TaskTagsVO> taskTagsVOPage = new Page<>(taskTagsPage.getCurrent(), taskTagsPage.getSize(), taskTagsPage.getTotal());
        if (CollectionUtils.isEmpty(taskTagsList)) {
            return taskTagsVOPage;
        }
        // todo other operation

        // 填充信息
        List<TaskTagsVO> taskTagsVOList = taskTagsList.stream().map(taskTags -> {
            TaskTagsVO taskTagsVO = TaskTagsVO.objToVo(taskTags);
            // todo other operation

            return taskTagsVO;
        }).collect(Collectors.toList());
        taskTagsVOPage.setRecords(taskTagsVOList);
        return taskTagsVOPage;
    }
}




